package com.greateamd.timetableasy.client.mvc.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.greateamd.timetableasy.client.domain.AuthType;
import com.greateamd.timetableasy.client.domain.EventType;
import com.greateamd.timetableasy.client.mvc.controller.IMainController;
import com.greateamd.timetableasy.client.mvc.model.Model;
import com.greateamd.timetableasy.client.mvc.model.PlanningModel;
import com.greateamd.timetableasy.client.mvc.view.crud.campus.AddCampusPage;
import com.greateamd.timetableasy.client.mvc.view.crud.course.AddCoursePage;
import com.greateamd.timetableasy.client.mvc.view.crud.cursus.AddCursusPage;
import com.greateamd.timetableasy.client.mvc.view.crud.event.AddEventPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.CampusPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.PromotionPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.StudyPeriodPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.TeacherPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.UniversityPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.planning.UserPlanningPage;
import com.greateamd.timetableasy.client.mvc.view.crud.promotion.AddPromotionPage;
import com.greateamd.timetableasy.client.mvc.view.crud.university.AddUniversityPage;
import com.greateamd.timetableasy.client.mvc.view.crud.university.EditUniversityPage;
import com.greateamd.timetableasy.client.mvc.view.crud.user.AddUserPage;
import com.greateamd.timetableasy.client.mvc.view.crud.user.EditUserPage;
import com.greateamd.timetableasy.client.mvc.view.home.HomePage;
import com.greateamd.timetableasy.domain.Auth;
import com.greateamd.timetableasy.domain.User;


public class MainView <M extends Model,C extends IMainController> extends Composite implements IMainView<M,C>{
	
	@UiTemplate("MainView.ui.xml")
	interface MainViewUIBinder extends UiBinder<Widget, MainView> {}
	private static MainViewUIBinder uiBinder = GWT.create(MainViewUIBinder.class);
	
	private final C controller;
	private final M model;
	private IPage currentPage = new HomePage<Model>();
	
	@UiField MenuItem menuPanelHomeBt;
	//
	@UiField MenuItem menuPanelAddUserBt;
	@UiField MenuItem menuPanelAddCampusBt;
	@UiField MenuItem menuPanelAddPromotionBt;
	@UiField MenuItem menuPanelAddCursusBt;
	@UiField MenuItem menuPanelAddUniversityBt;
	//
	@UiField MenuItem menuPanelEditUserBt;
	@UiField MenuItem menuPanelEditUniversityBt;
	//Event
	@UiField MenuItem mAddEventUserBt;
	@UiField MenuItem mAddEventCampusBt;
	@UiField MenuItem mAddEventPromotionBt;
	@UiField MenuItem mAddEventUniversityBt;
	@UiField MenuItem mAddEventSpBt;
	//Planning
	@UiField MenuItem menuPanelUserPlanningBt;
	@UiField MenuItem menuPanelPromotionPlanningBt;
	@UiField MenuItem menuPanelStudyPeriodPlanningBt;
	@UiField MenuItem menuPanelCampusPlanningBt;
	@UiField MenuItem menuPanelUniversityPlanningBt;
	
	@UiField SimplePanel panel;
	
	@UiField VerticalPanel authsPanel;
	@UiField Label loggedUsername;
	
	public MainView(M m,C c) {
		this.model = m;
		this.controller = c;
		initWidget(uiBinder.createAndBindUi(this));
		initGui();
	}

	private void initGui() {
		//
		menuPanelHomeBt.setCommand(onHomeBtClick);
		//Planning
		menuPanelUserPlanningBt.setCommand(onUserPlanningBtClick);
		menuPanelPromotionPlanningBt.setCommand(onPromotionPlanningBtClick);
		menuPanelStudyPeriodPlanningBt.setCommand(onStudyPeriodPlanningBtClick);
		menuPanelCampusPlanningBt.setCommand(onCampusPlanningBtClick);
		menuPanelUniversityPlanningBt.setCommand(onUniversityPlanningBtClick);
		//Add
		menuPanelAddUserBt.setCommand(onAddUserBtClick);
		menuPanelAddCampusBt.setCommand(onAddCampusBtClick);
		menuPanelAddCursusBt.setCommand(onAddCursusBtClick);
		menuPanelAddPromotionBt.setCommand(onAddPromotionBtClick);
		menuPanelAddUniversityBt.setCommand(onAddUniversityBtClick);
		//Edit
		menuPanelEditUserBt.setCommand(onEditUserBtClick);
		menuPanelEditUniversityBt.setCommand(onEditUniversityBtClick);
		//Event
		mAddEventUserBt.setCommand(onAddEventUserBtClick);
		mAddEventCampusBt.setCommand(onAddEventCampusBtClick);
		mAddEventPromotionBt.setCommand(onAddEventPromotionBtClick);
		mAddEventUniversityBt.setCommand(onAddEventUniversityBtClick);
		mAddEventSpBt.setCommand(onAddEventSpBtClick);
		
		RootPanel.get("PagePanel").add(this);
	}
	

	//---------------------------------- UI action
	
	private void addAdminView() {
		boolean contains = true;
		menuPanelAddUserBt.setVisible(contains);
		menuPanelAddCampusBt.setVisible(contains);
		menuPanelAddPromotionBt.setVisible(contains);
		menuPanelAddCursusBt.setVisible(contains);
		menuPanelAddUniversityBt.setVisible(contains);
		//
		menuPanelEditUserBt.setVisible(contains);
		menuPanelEditUniversityBt.setVisible(contains);
		//
		mAddEventUniversityBt.setVisible(contains);
		mAddEventSpBt.setVisible(contains);
		//
		menuPanelCampusPlanningBt.setVisible(contains);
		menuPanelStudyPeriodPlanningBt.setVisible(contains);
		menuPanelUniversityPlanningBt.setVisible(contains);
		menuPanelPromotionPlanningBt.setVisible(contains);
	}
	
	private void addUserView() {
		boolean contains = true;
		mAddEventUserBt.setVisible(contains);
		menuPanelUserPlanningBt.setVisible(contains);
		menuPanelCampusPlanningBt.setVisible(contains);
		menuPanelStudyPeriodPlanningBt.setVisible(contains);
		menuPanelUniversityPlanningBt.setVisible(contains);
		menuPanelPromotionPlanningBt.setVisible(contains);
	}
	
	private void addCampusManagerView() {
		boolean contains = true;
		menuPanelEditUserBt.setVisible(false);
		//
		mAddEventCampusBt.setVisible(contains);
		mAddEventPromotionBt.setVisible(contains);
		//
		menuPanelCampusPlanningBt.setVisible(contains);
		menuPanelStudyPeriodPlanningBt.setVisible(contains);
		menuPanelUniversityPlanningBt.setVisible(contains);
		menuPanelPromotionPlanningBt.setVisible(contains);
		
	}

	private void addTeacherView() {
		boolean contains = false;
		menuPanelAddUserBt.setVisible(contains);
		menuPanelAddCampusBt.setVisible(contains);
		menuPanelAddPromotionBt.setVisible(contains);
		menuPanelAddCursusBt.setVisible(contains);
		//
		menuPanelEditUserBt.setVisible(contains);
		menuPanelEditUniversityBt.setVisible(contains);
		//
		mAddEventCampusBt.setVisible(contains);
		mAddEventPromotionBt.setVisible(contains);
		mAddEventUniversityBt.setVisible(contains);
		mAddEventSpBt.setVisible(contains);
		//
		menuPanelCampusPlanningBt.setVisible(contains);
		menuPanelStudyPeriodPlanningBt.setVisible(contains);
		menuPanelUniversityPlanningBt.setVisible(contains);
		menuPanelUserPlanningBt.setVisible(true);
		menuPanelPromotionPlanningBt.setVisible(contains);
	}
	
	private void setNoAuthView() {
		boolean contains = false;
		menuPanelAddUserBt.setVisible(contains);
		menuPanelAddCampusBt.setVisible(contains);
		menuPanelAddPromotionBt.setVisible(contains);
		menuPanelAddCursusBt.setVisible(contains);
		menuPanelAddUniversityBt.setVisible(contains);
		//
		menuPanelEditUserBt.setVisible(contains);
		menuPanelEditUniversityBt.setVisible(contains);
		//
		mAddEventUserBt.setVisible(contains);
		mAddEventCampusBt.setVisible(contains);
		mAddEventPromotionBt.setVisible(contains);
		mAddEventUniversityBt.setVisible(contains);
		mAddEventSpBt.setVisible(contains);
		//
		menuPanelCampusPlanningBt.setVisible(contains);
		menuPanelStudyPeriodPlanningBt.setVisible(contains);
		menuPanelUniversityPlanningBt.setVisible(contains);
		menuPanelUserPlanningBt.setVisible(contains);
		menuPanelPromotionPlanningBt.setVisible(contains);
	}
	
	//---------------------------------- Event

	@Override
	public void onUserChange(User user) {
		if(user == null)
			return;
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_USER))){
			addUserView();
			authsPanel.add(new Label("-User"));
		}
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_ADMIN))){
			addAdminView();
			authsPanel.add(new Label("-Admin"));
		}
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_CAMPUS_MANAGER))){
			addCampusManagerView();
			authsPanel.add(new Label("-CManager"));
		}
		if(user.getAuths().contains(new Auth(model.getUser(),AuthType.ROLE_TEACHER))){
			addTeacherView();
			authsPanel.add(new Label("-Teacher"));
		}
		loggedUsername.setText("Hi, "+user.getUsername());
		currentPage.setUser(user);
	}

	@Override
	public void onUserChange() {
		onUserChange(model.getUser());
	}
	
	//---------------------------------- Interface behavior
	
	@Override
	public void setHomePage(M model) {
		currentPage = new HomePage<M>();
		panel.setWidget(currentPage);
	}

	@Override
	public void setAddUserPage(M model) {
		currentPage = new AddUserPage<M>();
		panel.setWidget(currentPage);
	}

	@Override
	public void setAddCampusPage(M model) {
		currentPage = new AddCampusPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setAddCoursePage(M model) {
		currentPage = new AddCoursePage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setAddPromotionPage(M model) {
		currentPage = new AddPromotionPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setAddCursusPage(M model) {
		currentPage = new AddCursusPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setAddUniversityPage(M model) {
		currentPage = new AddUniversityPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setEditUserPage(M model) {
		currentPage = new EditUserPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setEditUniversityPage(M model) {
		currentPage = new EditUniversityPage<M>();
		panel.setWidget(currentPage);
	}
	
	@Override
	public void setAddEventPage(M model,EventType type) {
		currentPage = new AddEventPage<Model>(model,type);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setUserPlanningPage(final T model) {
		currentPage = new UserPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setTeacherPlanningPage(final T model) {
		currentPage = new TeacherPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setPromotionPlanningPage(final T model) {
		currentPage = new PromotionPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setStudyPeriodPlanningPage(final T model) {
		currentPage = new StudyPeriodPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setCampusPlanningPage(final T model) {
		currentPage = new CampusPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	@Override
	public <T extends PlanningModel> void setUniversityPlanningPage(final T model) {
		currentPage = new UniversityPlanningPage<T>(model);
		panel.setWidget(currentPage);
	}
	
	//--------------------------------------Ui Handler
	
	private final Command onHomeBtClick = new Command() {
			@Override
			public void execute() {
				controller.onPageRequest(TimeTableasyPage.HOME);
			}};
			
			
	//-------------------------- Add page
	private final Command onAddUserBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_USER);
		}};
	private final Command onAddCampusBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_CAMPUS);
		}};
	private final Command onAddCourseBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_COURSE);
		}};
	private final Command onAddPromotionBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_PROMOTION);
		}};
	private final Command onAddCursusBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_CURSUS);
		}};
	private final Command onAddUniversityBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_UNIVERSITY);
		}};
		
	//-------------------------- Edit page
	private final Command onEditUserBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.EDIT_USER);
		}};
	private final Command onEditUniversityBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.EDIT_UNIVERSITY);
		}};
		
		
	//-------------------------- View planning
	private final Command onUserPlanningBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.PLANNING_USER);
		}};
	private final Command onPromotionPlanningBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.PLANNING_PROMOTION);
		}};
	private final Command onStudyPeriodPlanningBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.PLANNING_STUDYPERIOD);
		}};
	private final Command onCampusPlanningBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.PLANNING_CAMPUS);
		}};
	private final Command onUniversityPlanningBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.PLANNING_UNIVERSITY);
		}};
	
	//-------------------------- Add event
	private final Command onAddEventUserBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_EVENT_USER);
		}};	
	private final Command onAddEventCampusBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_EVENT_CAMPUS);
		}};
	private final Command onAddEventUniversityBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_EVENT_UNIVERSITY);
		}};
	private final Command onAddEventPromotionBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_EVENT_PROMOTION);
		}};
	private final Command onAddEventSpBtClick = new Command() {
		@Override
		public void execute() {
			controller.onPageRequest(TimeTableasyPage.ADD_EVENT_SP);
		}};
}
